package weblicitacao

import org.springframework.dao.DataIntegrityViolationException

class LicitacaoController {

	static navigation = [
		group:'tabs',
		order:2,
		title:'Licitacao',
		action:'index'
	]

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [licitacaoInstanceList: Licitacao.list(params), licitacaoInstanceTotal: Licitacao.count()]
    }

    def create() {
        [licitacaoInstance: new Licitacao(params)]
    }

    def save() {
        def licitacaoInstance = new Licitacao(params)
        if (!licitacaoInstance.save(flush: true)) {
            render(view: "create", model: [licitacaoInstance: licitacaoInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), licitacaoInstance.id])
        redirect(action: "show", id: licitacaoInstance.id)
    }

    def show() {
		def licitacaoInstance = Licitacao.get(params.id)
		if (licitacaoInstance.tipoItens == 'Produto') {
			redirect(action: "show1", params: params)
		}
		else {
			redirect(action: "show2", params: params)
		}
    }

    def show1() {
        def licitacaoInstance = Licitacao.get(params.id)
        if (!licitacaoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "list")
            return
        }

        [licitacaoInstance: licitacaoInstance]
    }

    def show2() {
        def licitacaoInstance = Licitacao.get(params.id)
        if (!licitacaoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "list")
            return
        }

        [licitacaoInstance: licitacaoInstance]
    }

    def edit() {
        def licitacaoInstance = Licitacao.get(params.id)
        if (!licitacaoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "list")
            return
        }

        [licitacaoInstance: licitacaoInstance]
    }

    def update() {
        def licitacaoInstance = Licitacao.get(params.id)
        if (!licitacaoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (licitacaoInstance.version > version) {
                licitacaoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'licitacao.label', default: 'Licitacao')] as Object[],
                          "Another user has updated this Licitacao while you were editing")
                render(view: "edit", model: [licitacaoInstance: licitacaoInstance])
                return
            }
        }

        licitacaoInstance.properties = params

        if (!licitacaoInstance.save(flush: true)) {
            render(view: "edit", model: [licitacaoInstance: licitacaoInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), licitacaoInstance.id])
        redirect(action: "show", id: licitacaoInstance.id)
    }

    def delete() {
        def licitacaoInstance = Licitacao.get(params.id)
        if (!licitacaoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "list")
            return
        }

        try {
            licitacaoInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'licitacao.label', default: 'Licitacao'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
